package com.item.controller;


import com.item.bean.Result;
import com.item.domain.TableToList;
import com.item.service.TableToListService;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class TableToListController {

    @Autowired
    TableToListService tableToListService;

    @PostMapping(value = "/tableToList/{limit}/{page}")
    public Result TableToList(
            @PathVariable(value = "limit", required = false) int limit,
            @PathVariable(value = "page", required = false) int page,
            @RequestBody(required = false) TableToList tableToList) {
        Result result = new Result();

        List<TableToList> tableToLists = tableToListService.findAll(limit, page, tableToList);

        if (tableToLists != null) {
            result.setFlag(true);
            result.setMessage("数据请求成功");
            result.setData(tableToLists);
        } else {
            result.setFlag(false);
            result.setMessage("数据请求失败");
            result.setData(new ArrayList<>());
        }

        return result;
    }

    @PostMapping(value = "/table/to/list/add")
    public Integer TableToListAdd(@RequestBody TableToList tableToList) {

        int i = tableToListService.add(tableToList);
        return i;
    }

    @PutMapping(value = "/table/to/list/update")
    public Integer tableToListUpdate(@RequestBody TableToList tableToList) {

        int i = tableToListService.update(tableToList);
        return i;
    }

    @DeleteMapping(value = "/tableToList/{id}")
    public Integer tableToListDelete(@PathVariable(value = "id") int id) {

        int i = tableToListService.delete(id);
        return i;
    }

}
